package com.ucar.ui.login;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ucar.R;
import com.ucar.net.BaseResponse;
import com.ucar.net.Callback;
import com.ucar.net.NetRequest;
import com.ucar.ui.base.BaseActivity;
import com.ucar.ui.main.MainActivity;
import com.ucar.util.CountDownTimer;

public class LoginActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = LoginActivity.class.getSimpleName();

	
	private Button btnCommit;
	private Button btnGetCode;
	private EditText etPhone;
	private EditText etCode;
//	private String code;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBaseTitleContent( R.layout.activity_login, R.string.title_login);
		if( mSpUtil.isLogin() ){
			redirectActivity( MainActivity.class );
			finish();
		}
	}

	@Override
	public void onInit() {
		setLeftImage( R.drawable.btn_close );
	}

	@Override
	public void onFindViews() {
		btnCommit = find( R.id.btn_commit );
		btnGetCode = find( R.id.btn_get_code );
		etPhone = find( R.id.et_phone);
		etCode = find( R.id.et_code);
	}

	@Override
	public void onInitViewData() {
	}

	@Override
	public void onBindListener() {
		btnGetCode.setOnClickListener( this );
		btnCommit.setOnClickListener( this );
	}

	@Override
	public void onClick(View v) {
		String mobileNum = etPhone.getText().toString().trim();
		switch (v.getId()) {
		case R.id.btn_commit:
			String codeNum = etCode.getText().toString().trim();
			
			if( TextUtils.isEmpty(mobileNum) ){
				showToast( R.string.input_phone_num);
				return;
			}else if( TextUtils.isEmpty(codeNum) ){
				showToast( R.string.input_auth_code);
				return;
			}
			requestLogin();
			break;
		case R.id.btn_get_code:
			if ( TextUtils.isEmpty( mobileNum ) )
			{
				showToast( R.string.input_phone_num );
				return;
			}
			requestCode( mobileNum );
			break;
		}
	}

	/**
	 * 请求验证码
	 * @param mobileNum
	 */
	private void requestCode( final String mobileNum ) 
	{
		// Tag used to cancel the request
		String tag = "tag_request_code";
		NetRequest.getInstance(this).get(new Callback() {

			@Override
			public void onSuccess(BaseResponse response) 
			{
//				code = response.getString("result");
				btnGetCode.setEnabled( false );
				btnGetCode.setTextColor( getResources().getColor( R.color.app_gray ) );
				new CountDownTimer( 60000, 1000 ) {
					
					@Override
					public void onTick(long millisUntilFinished) 
					{
						btnGetCode.setText( ( millisUntilFinished / 1000 )  + "s" );
					}
					
					@Override
					public void onFinish() 
					{
						btnGetCode.setTextColor( getResources().getColor( R.color.app_black ) );
						btnGetCode.setText( R.string.obtain_auth_code );
						btnGetCode.setEnabled( true );
					}
				}.start();
			}

			@Override
			public void onPreExecute() {
				toShowProgressMsg( getString( R.string.getting ) );
			}

			@Override
			public void onFinished() {
				toCloseProgressMsg();
			}

			@Override
			public void onFailed(String msg) {

			}

			@Override
			public Map<String, String> getParams() {
				Map<String, String> params = new HashMap<String, String>();
				params.put("url", "app/getcercode");
				params.put("mobilenum", mobileNum );
				return params;
			}
		}, tag);
	}

	/**
	 * 登录
	 */
	private void requestLogin() {
		// Tag used to cancel the request
		String tag = "tag_request_login";
		NetRequest.getInstance(this).get(new Callback() {

			@Override
			public void onSuccess(BaseResponse response) {
				mSpUtil.setLogin(true);
				JSONObject jsonObj = response.getData();
				mSpUtil.setToken(jsonObj.optString("token"));
				mSpUtil.setName(jsonObj.optString("name"));
				mSpUtil.setCarFlag(jsonObj.optString("carflag"));
				mSpUtil.setRefCode(jsonObj.optString("refcode"));
				
				JSONObject userObj = jsonObj.optJSONObject("users");
				mSpUtil.setMobile(userObj.optString("mobile"));
				mSpUtil.setGender(userObj.optString("gander"));
//				redirectActivity( MainActivity.class );
				finish();
			}

			@Override
			public void onPreExecute() {
				toShowProgressMsg(getString(R.string.login));
			}

			@Override
			public void onFinished() {
				toCloseProgressMsg();
			}

			@Override
			public void onFailed(String msg) {
				showToast(msg);
			}

			@Override
			public Map<String, String> getParams() {
				Map<String, String> params = new HashMap<String, String>();   
				params.put("url", "app/verifycode");
				params.put("mobilenum", etPhone.getText().toString().trim());
				params.put("code", etCode.getText().toString().trim());
				return params;
			}
		}, tag);
	}
	
}
